#include "game.h"

//extern int gamestate;

#ifdef USE_MIXER
extern Mix_Music *music;
extern int vol_sfx, vol_music;
#endif

#ifdef USE_GL
extern GLuint cursor;
#else
extern SDL_Surface *cursor, *screen;
#endif

extern int mx,my;

extern char error_message[256];

int game_main()
{
	int ret = 1;

// TODO: This is where the main game loop goes.
//  There is nothing here for now, because it is totally dependent on
//  whatever you're writing.
//  So we will put a cursor and leave it at that.

	//Enable texturing
	glEnable(GL_TEXTURE_2D);

#ifdef USE_MIXER
	/* This is the music to play. */
	if (vol_music) {
		music = Mix_LoadMUS("audio/game.mid");
		if (music == NULL)
			printf("Error loading music: %s\n",Mix_GetError());
		else {
			Mix_PlayMusic(music, -1);
			Mix_VolumeMusic(vol_music);
		}
	}
#endif

	int done = 0;
	while (!done && ret)
	{
		// DRAW cursor
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		//glBindTexture(GL_TEXTURE_2D, cursor);
		/* glBegin(GL_QUADS);
		glTexCoord2f(0,0);
		glVertex2i(mx, my);
		glTexCoord2f(1,0);
		glVertex2i(mx+32, my);
		glTexCoord2f(1,1);
		glVertex2i(mx+32, my+32);
		glTexCoord2f(0,1);
		glVertex2i(mx, my+32);
		glEnd(); */

		//Flip the backbuffer to the primary
		SDL_GL_SwapBuffers();
		/* Don't run too fast */
		SDL_Delay (1);

		SDL_Event event;
		/* Check for events */
// TODO:  If you want the program to shutdown, return false
//  If you want to change to a different subscreen, set gamestate to
//   something else, then return true.
//  To throw an error, put something into error_message, set gamestate
//   to GAMESTATE_ERROR, then return true.
		while (SDL_PollEvent (&event))
		{
			switch (event.type)
			{
				case SDL_KEYUP:
					if (event.key.keysym.sym == SDLK_ESCAPE)
					{
//						gamestate = GAMESTATE_MENU;
						done=1;
					}
					break;
				case SDL_MOUSEMOTION:
					mx=event.motion.x;
					my=event.motion.y;
					break;
				case SDL_QUIT:
					done=1;
					break;
				default:
					break;
			}
		}
	}

#ifdef USE_MIXER
	Mix_HaltMusic();
	Mix_FreeMusic(music);
	music = NULL;
#endif

	return ret;
}

